using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Xml;

namespace ClassLibrary
{
    public partial class ImageDataController
    {
        public static ImageData CreateImageData(string ImageId, string UserId, string ImagePath, int PatientId, string Bodypart, string ReasonForVisit, string Diagnosis, bool ConsentForResearch, bool ConsentForEducation, bool ConsentForStorage)
        {
            try
            {
                ImageData iData = new ImageData()
                {
                    ImageId = ImageId,
                    UserId = UserId,
                    ImagePath = ImagePath,
                    PatientId = PatientId,
                    Bodypart = Bodypart,
                    ReasonForVisit = ReasonForVisit,
                    Diagnosis = Diagnosis,
                    ConsentForResearch = ConsentForResearch,
                    ConsentForEducation = ConsentForEducation,
                    ConsentForStorage = ConsentForStorage
                };

                SqlCommand cmd = DBHandler.getCommand("sp_Insert_ImageData");
                DBHandler.AddParameter(ref cmd, "@ImageId", iData.ImageId);
                DBHandler.AddParameter(ref cmd, "@UserId", iData.UserId);
                DBHandler.AddParameter(ref cmd, "@ImagePath", iData.ImagePath);
                DBHandler.AddParameter(ref cmd, "@PatientId", iData.PatientId);
                DBHandler.AddParameter(ref cmd, "@BodyPart", iData.Bodypart);
                DBHandler.AddParameter(ref cmd, "@ReasonForVisit", iData.ReasonForVisit);
                DBHandler.AddParameter(ref cmd, "@Diagnosis", iData.Diagnosis);
                DBHandler.AddParameter(ref cmd, "@ConsentForResearch", Convert.ToInt16(iData.ConsentForResearch));
                DBHandler.AddParameter(ref cmd, "@ConsentForEducation", Convert.ToInt16(iData.ConsentForEducation));
                DBHandler.AddParameter(ref cmd, "@ConsentForStorage", Convert.ToInt16(iData.ConsentForStorage));
                DBHandler.ExecuteSPNonQuery(cmd);

                return iData;
            }
            catch (Exception e)
            {
                LogWriter.Print("Exception: ImageDataController->CreateImageData() " + e, LogLevel.EXCEPTION);
            }

            return null;
        }

        public static void CreateEmptyImageData(string ImageId, string UserId, string ImagePath)
        {
            try
            {
                LogWriter.Print("Creating empty imagedata for user: " + UserId, LogLevel.LOG);
                SqlCommand cmd = DBHandler.getCommand("sp_Insert_ImageData");
                DBHandler.AddParameter(ref cmd, "@ImageId", ImageId);
                DBHandler.AddParameter(ref cmd, "@UserId", UserId);
                DBHandler.AddParameter(ref cmd, "@ImagePath", ImagePath);
                DBHandler.AddParameter(ref cmd, "@PatientId", "");
                DBHandler.AddParameter(ref cmd, "@BodyPart", "");
                DBHandler.AddParameter(ref cmd, "@ReasonForVisit", "");
                DBHandler.AddParameter(ref cmd, "@Diagnosis", "");
                DBHandler.AddParameter(ref cmd, "@ConsentForResearch", "");
                DBHandler.AddParameter(ref cmd, "@ConsentForEducation", "");
                DBHandler.AddParameter(ref cmd, "@ConsentForStorage", "");
                DBHandler.ExecuteSPNonQuery(cmd);
            }
            catch (Exception e)
            {
                LogWriter.Print("Exception: ImageDataController->CreateEmptyImageData() " + e, LogLevel.EXCEPTION);
            }
        }
        
        public static List<ImageData> GetAllImageData(Guid UserId)
        {
            List<ImageData> imageDataList = new List<ImageData>();
            try
            {
                SqlCommand cmd = DBHandler.getCommand("sp_Get_AllImagesForWebUser");
                DBHandler.AddParameter(ref cmd, "@UserId", UserId);
                foreach (DataRow row in DBHandler.ExecuteSP(cmd).Tables[0].Rows)
                {
                    imageDataList.Add(
                            new ImageData(row["ImageId"].ToString(), row["UserId"].ToString(), row["ImagePath"].ToString(), (int)row["PatientId"], row["BodyPart"].ToString(), row["ReasonForVisit"].ToString(), row["Diagnosis"].ToString(), Convert.ToBoolean(row["ConsentForResearch"]), Convert.ToBoolean(row["ConsentForEducation"]), Convert.ToBoolean(row["ConsentForStorage"]))
                        );
                }
            }
            catch (Exception e)
            {
                LogWriter.Print("Exception: ImageDataController->GetAllImageData() " + e, LogLevel.EXCEPTION);
            }
            return imageDataList;
        }

        public static List<string> GetAllImageDataId(Guid UserId)
        {
            List<string> imageDataList = new List<string>();
            try
            {
                SqlCommand cmd = DBHandler.getCommand("sp_Get_AllImagesForWebUser");
                DBHandler.AddParameter(ref cmd, "@UserId", UserId);
                foreach (DataRow row in DBHandler.ExecuteSP(cmd).Tables[0].Rows)
                {
                    LogWriter.Print("imagedatacontroller() new Guid(row[ImageId].ToString()): " + new Guid(row["ImageId"].ToString()), LogLevel.DEBUG);
                    imageDataList.Add(row["ImageId"].ToString());
                }
            }
            catch (Exception e)
            {
                LogWriter.Print("Exception: ImageDataController->GetAllImageData() " + e, LogLevel.EXCEPTION);
            }
            return imageDataList;
        }

        public static void UpdateImageData(string ImageId, string PatientId, string BodyPart, string ReasonForVisit, string Diagnosis, string ConsentForResearch, string ConsentForEducation, string ConsentForStorage)
        {
            try
            {
                LogWriter.Print("Updating ImageData - " + ImageId, LogLevel.LOG);
                long PId = Convert.ToInt64(PatientId);
                SqlCommand cmd = DBHandler.getCommand("sp_Update_ImageData");
                DBHandler.AddParameter(ref cmd, "@ImageId", new Guid(ImageId));
                DBHandler.AddParameter(ref cmd, "@PatientId", PId);
                DBHandler.AddParameter(ref cmd, "@BodyPart", BodyPart);
                DBHandler.AddParameter(ref cmd, "@ReasonForVisit", ReasonForVisit);
                DBHandler.AddParameter(ref cmd, "@Diagnosis", Diagnosis);
                DBHandler.AddParameter(ref cmd, "@ConsentForResearch", ConsentForResearch);
                DBHandler.AddParameter(ref cmd, "@ConsentForEducation", ConsentForEducation);
                DBHandler.AddParameter(ref cmd, "@ConsentForStorage", ConsentForStorage);
                DBHandler.ExecuteSPNonQuery(cmd);
            }
            catch (Exception exc)
            {
                LogWriter.Print("Exception: ImageDataController->UpdateImageData() " + exc, LogLevel.EXCEPTION);
            }
        }

        public static void DeleteImageData(Guid ImageId)
        {
            try
            {
                SqlCommand cmd = DBHandler.getCommand("sp_Delete_ImageData");
                DBHandler.AddParameter(ref cmd, "@ImageId", ImageId);
                DBHandler.ExecuteSPNonQuery(cmd);
            }
            catch (Exception e)
            {
                LogWriter.Print("Exception: ImageDataController->DeleteImageData() " + e, LogLevel.EXCEPTION);
            }
        }
    }
}

